package com.aier.common.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.aier.admin.entity.SysCity;
import com.aier.common.service.ISysCityService;


public class CityUtil {
    
    private static Map<Integer,String> province;
    
    private static Map<Integer,CityBean> province_all;
    
    private static Map<Integer,Map<Integer,String>> city;
    
    private static Map<Integer,List<CityBean>> city_data;
    
    private static Map<Integer,Map<Integer,String>> city_data_map;
    
    private static Map<String,Integer> city_string;
    
    private static Map<Integer,String> city_id;
    
    private static Map<Integer,CityBean> city_all;
    

    /**
     * 初始化加载城市数据
     */
    public static void initLoad(ISysCityService sysCityService){
        loadProvince(sysCityService);
        loadCity(sysCityService);
        loadArea(sysCityService);
        loadTown(sysCityService);
        loadCityData(sysCityService);
    }
    
    /**
     * 加载省数据
     * @param sysCityService
     */
    private static void loadProvince(ISysCityService sysCityService){
        province=new HashMap<Integer, String>();
        province_all=new HashMap<Integer,CityBean>();
        List<SysCity> listP = sysCityService.searchAllProvince(ISysCityService.ON);
        if(null==listP||listP.size()<=0){
            return;
        }
        for(SysCity sc:listP){
            CityBean cb = toCityBean(sc);
            province_all.put(cb.getId(), cb);
            province.put(cb.getId(),cb.getName());
        }
    }
    
    /**
     * 加载城市数据
     * @param sysCityService
     */
    private static void loadCity(ISysCityService sysCityService){
        city=new HashMap<Integer, Map<Integer,String>>();
        if(null==province||province.size()<=0){
            return;
        }
        loadCityDate(sysCityService, city,province);
    }
    
    /**
     * 加载区域数据
     * @param sysCityService
     */
    private static void loadArea(ISysCityService sysCityService){
        List<SysCity> list=sysCityService.searchCityCtype(ISysCityService.CTYPE_CITY);
        if(null==list||list.size()<=0){
            return;
        }
        loadCityDate(sysCityService, city,list);
    }
    
    /**
     * 加载区域村镇
     * @param sysCityService
     */
    private static void loadTown(ISysCityService sysCityService){
        List<SysCity> list=sysCityService.searchCityCtype(ISysCityService.CTYPE_AREA);
        if(null==list||list.size()<=0){
            return;
        }
        loadCityDate(sysCityService, city,list);
    }
    
    private static void loadCityDate(ISysCityService sysCityService,Map<Integer,Map<Integer,String>> sub,List<SysCity> list){
        for(SysCity tsc:list){
            Map<Integer,String> cmap=new HashMap<Integer, String>();
            List<SysCity> lsc=sysCityService.searchCitySub(tsc.getCid(),ISysCityService.ON);
            for(SysCity sc:lsc){
                cmap.put(sc.getCid(),sc.getName());
            }
            sub.put(tsc.getCid(),cmap);
        }
    }
    
    private static void loadCityDate(ISysCityService sysCityService,Map<Integer,Map<Integer,String>> sub,Map<Integer,String> city){
        for(Integer pid:city.keySet()){
            Map<Integer,String> cmap=new HashMap<Integer, String>();
            List<SysCity> lsc=sysCityService.searchCitySub(pid,ISysCityService.ON);
            for(SysCity sc:lsc){
                cmap.put(sc.getCid(),sc.getName());
            }
            sub.put(pid,cmap);
        }
    }
    
    private static void loadCityData(ISysCityService sysCityService){
        city_data=new HashMap<Integer, List<CityBean>>();
        city_data_map=new HashMap<Integer, Map<Integer,String>>();
        city_string=new HashMap<String, Integer>();
        city_id=new HashMap<Integer, String>();
        city_all=new HashMap<Integer,CityBean>();
        List<SysCity> list=sysCityService.searchAll(ISysCityService.ON);
        if(null==list||list.size()<=0){
            return;
        }
        for(SysCity sc:list){
            List<SysCity> lc=sysCityService.searchCitySub(sc.getCid(),ISysCityService.ON);
            List<CityBean> la=new ArrayList<CityBean>();
            Map<Integer,String> m=new HashMap<Integer, String>();
            for(SysCity tsc:lc){
                la.add(toCityBean(tsc));
                m.put(tsc.getCid(),tsc.getName());
                city_string.put(tsc.getName(), tsc.getCid());
                city_id.put(sc.getCid(), sc.getName());
            }
            city_data.put(sc.getCid(),la);
            city_data_map.put(sc.getCid(),m);
            
            city_string.put(sc.getName(), sc.getCid());
            city_id.put(sc.getCid(), sc.getName());
            city_all.put(sc.getCid(),toCityBean(sc));
        }
    }
    
    /**
     * 转换成CityBean数据
     * 
     * @param sc
     * @return
     */
    private static CityBean toCityBean(SysCity sc) {
        CityBean proB = new CityBean();
        proB.setId(sc.getCid());
        proB.setName(sc.getName());
        proB.setSubid(sc.getSubCid());
        proB.setPinJ(sc.getPinJ());
        proB.setPinA(sc.getPinA());
        proB.setCtype(sc.getCtype());
        return proB;
    }
    
    /**
     * 根据cid获取地址
     * @param cid
     * @return
     */
    public static String addressDetail(int cid) {
        String address = "";
        CityBean cb = CityUtil.getCityBean(cid);
        if (null == cb) {
            return address;
        }
        Integer subid = cb.getSubid();
        Map<Integer, String> map;
        Integer mapKey = null;
        switch (cb.getCtype()) {
            case ISysCityService.CTYPE_TOWN:
                map = CityUtil.getSubCityMap(subid);
                mapKey = cb.getId();
                cb = CityUtil.getCityBean(cb.getSubid());
                subid = cb.getSubid();
                address = map.get(mapKey) + address;
            case ISysCityService.CTYPE_AREA:
                map = CityUtil.getSubCityMap(subid);
                mapKey = cb.getId();
                cb = CityUtil.getCityBean(cb.getSubid());
                subid = cb.getSubid();
                address = map.get(mapKey) + address;
            case ISysCityService.CTYPE_CITY:
                map = CityUtil.getSubCityMap(subid);
                mapKey = cb.getId();
                subid = CityUtil.getCityBean(mapKey).getSubid();
                address = map.get(mapKey) + address;
            case ISysCityService.CTYPE_PROVINCE:
                map = CityUtil.getProvince();
                if (subid > 0) {
                    mapKey = subid;
                } else if (cid > 0) {
                    mapKey = cid;
                }
                address = map.get(mapKey) + address;
                break;
            default:
                return address;
        }
        return address;
    }
    
    public static String getCityListToString(Integer cid){
        return cityListToString(cid);
    }
    
    public static CityBean getCityBean(Integer cid){
        return city_all.get(cid);
    }
    
    private static String cityListToString(Integer cid){
        CityBean cb=city_all.get(cid);
        if(null==cb){
            return "";
        }else if(cb.getSubid()>0){
            return cityListToString(cb.getSubid())+cb.getName();
        }
        return cb.getName();
    }

    public static Map<Integer, String> getProvince() {
        return province;
    }

    public static Map<Integer, CityBean> getProvinceAll() {
        return province_all;
    }
    
    public static Map<Integer,String> getCity(Integer pid){
        return city.get(pid);
    }
    
    public static List<CityBean> getSubCity(Integer cid){
        return city_data.get(cid);
    }
    
    public static Map<Integer,String> getSubCityMap(Integer cid){
        return city_data_map.get(cid);
    }

    public static Map<String, Integer> getCity_string() {
        return city_string;
    }

    public static Map<Integer, String> getCity_id() {
        return city_id;
    }

}
